#include <stdio.h>
#include <stdlib.h>


struct node {
    struct node* previous;
    char character;
    struct node* next;
};


struct node *create_list() {
    struct node* head = (struct node*)malloc(sizeof(struct node));
    if (head) {
        head->previous = NULL;
        head->character = 'z';
        head->next = NULL;
        return head;
    }
    else {
        return NULL;
    }
}


void insert_node(struct node* pos, char data) {
    struct node* new_node = (struct node*)malloc(sizeof(struct node));
    new_node->character = data;

    if (pos->next) {
        new_node->next = pos->next;
        new_node->previous = pos;
        pos->next->previous = new_node;
        pos->next = new_node;
    }
    else {
        new_node->previous = pos;
        pos->next = new_node;
        new_node->next = NULL;
    }
}


struct node *delete_node(struct node *pos) {
    if (pos && pos->previous) {
        if (pos->next) {
            struct node *new_cursor = pos->previous;
            pos->next->previous = pos->previous;
            pos->previous->next = pos->next;
            free(pos);
            return new_cursor;
        }
        else {
            struct node* new_cursor = pos->previous;
            pos->previous->next = NULL;
            free(pos);
            return new_cursor;
        }
    }
    return pos;
}


void toString(struct node* link_table, char str[]) {
    struct node* end = link_table;
    int length = 0;
    if (end->next) {
        while (end->next) {
            end = end->next;
            str[length] = end->character;
            length++;
        }
        str[length] = ' ';
    }
}


void printList(struct node* L) {
    struct node* end = L;
    while (end->next) {
        end = end->next;
        printf("%c", end->character);
    }
    printf("\n");
}


//int main() {
//    struct node* link_table = create_list();
//    struct node* pos = link_table;
//
//    char data[10];
//    data[0] = 'a';
//    printf("pos: %c\n", pos->character);
//    insert_node(pos, data[0]);
//    printList(link_table);
//
//    pos = pos->next;
//    data[1] = 'b';
//    printf("pos: %c\n", pos->character);
//    insert_node(pos, data[1]);
//    printList(link_table);
//
//    pos = pos->previous;
//    data[2] = 'c';
//    printf("pos: %c\n", pos->character);
//    insert_node(pos, data[2]);
//    printList(link_table);
//
//    data[3] = 'd';
//    printf("pos: %c\n", pos->character);
//    insert_node(pos, data[3]);
//    printList(link_table);
//
//    pos = pos->next;
//    data[4] = 'e';
//    printf("pos: %c\n", pos->character);
//    insert_node(pos, data[4]);
//    printList(link_table);
//    
//    pos = delete_node(pos->previous);
//    printList(link_table);
//    return 0;
//}
